package com.example.administrator.daydayappmarket.search.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.administrator.daydayappmarket.Contacts;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.recommend.activity.RecommendMainThirdActivity;
import com.example.administrator.daydayappmarket.recommend.view.MyGridView;
import com.example.administrator.daydayappmarket.search.moduls.SearchActivityApp;
import com.example.administrator.daydayappmarket.search.moduls.SearchActivityParent;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Administrator on 2017/1/13.
 */

public class SearchFragmentFirst1Adapter extends BaseAdapter {
    private List<List<SearchActivityParent>> data;
    private Context context;

    public SearchFragmentFirst1Adapter(Context context, List<List<SearchActivityParent>> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public List<SearchActivityParent> getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        String theme_type = data.get(position).get(0).getTheme_type();
        if (theme_type.equals("s_hotword")) {
            return 0;
        } else if (theme_type.equals("s_game_recommend")) {
            return 1;
        } else if (theme_type.equals("s_software_recommend")) {
            return 2;
        }

        return super.getItemViewType(position);
    }

    @Override
    public int getViewTypeCount() {

        return 3;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_search_fragment_first_1_listview, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        switch (getItemViewType(position)) {
            case 0:
                viewHolder.textView.setText("HOT");
                final List<SearchActivityParent> item = getItem(position);
                SearchFragmentFirst1GridViewAdapter adapter = new SearchFragmentFirst1GridViewAdapter(context,item);
                viewHolder.gridView.setAdapter(adapter);

                viewHolder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent("com.appMarket.searchActivity.adapterFirst");
                        intent.putExtra("name",item.get(position).getTitle());
                        context.sendBroadcast(intent);
                    }
                });
                break;
            case 1:
                viewHolder.textView.setText("游戏");
                final List<SearchActivityParent> item1 = getItem(position);
                SearchFragmentFirst1GridViewAdapter adapter1 = new SearchFragmentFirst1GridViewAdapter(context,item1);
                viewHolder.gridView.setAdapter(adapter1);

                viewHolder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        //点击跳转到recommend的第三季页面
                        Intent intent = new Intent(context, RecommendMainThirdActivity.class);
                        SearchActivityApp app1= (SearchActivityApp) item1.get(position);
                        intent.putExtra("id",app1.getId());
                        intent.putExtra("type",app1.getApps_type());
                        context.startActivity(intent);
                    }
                });


                break;
            case 2:
                viewHolder.textView.setText("软件");
                final List<SearchActivityParent> item2 = getItem(position);
                SearchFragmentFirst1GridViewAdapter adapter2 = new SearchFragmentFirst1GridViewAdapter(context,item2);
                viewHolder.gridView.setAdapter(adapter2);
                viewHolder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        //点击跳转到recommend的第三季页面
                        Intent intent = new Intent(context, RecommendMainThirdActivity.class);
                        SearchActivityApp app1= (SearchActivityApp) item2.get(position);
                        intent.putExtra("id",app1.getId());
                        intent.putExtra("type",app1.getApps_type());
                        context.startActivity(intent);
                    }
                });
                break;
        }

        return convertView;
    }


    static class ViewHolder {
        @InjectView(R.id.item_search_fragment_first_1_listVIew_textView)
        TextView textView;
        @InjectView(R.id.item_search_fragment_first_1_listVIew_gridView)
        MyGridView gridView;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
            textView = (TextView) view.findViewById(R.id.item_search_fragment_first_1_listVIew_textView);
            gridView = (MyGridView) view.findViewById(R.id.item_search_fragment_first_1_listVIew_gridView);
        }
    }
}
